package com.mm.mweather.util

import android.text.TextUtils
import com.google.gson.Gson
import com.mm.mweather.db.City
import com.mm.mweather.db.Country
import com.mm.mweather.db.Province
import com.mm.mweather.gson.Aqi
import com.mm.mweather.gson.Weather
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject

object Utility {
    /*
    返回省级数据
     */
   fun handleProvinceResponse(respose:String):Boolean{
        if (!TextUtils.isEmpty(respose)){
            try {
                val  allProvince :JSONArray = JSONArray(respose)
                for ( i in 0..(allProvince.length()-1)){
                    var provinceObject: JSONObject = allProvince.get(i) as JSONObject
                    var province:Province= Province()
                    province.provinceName=provinceObject.getString("name")
                    province.id = provinceObject.getInt("id")
                    province.save()

                }
                return true
            }
            catch (e: JSONException){
                e.printStackTrace()
            }
        }
        return false
    }
    /*
   返回市级数据
    */
    fun handleCityResponse(respose:String,provinceId:Int):Boolean{
        if (!TextUtils.isEmpty(respose)){
            try {
                val  allCity :JSONArray = JSONArray(respose)
                for ( i in 0..(allCity.length()-1)){
                    var cityObject: JSONObject = allCity.get(i) as JSONObject
                    var city:City= City()
                    city.cityName=cityObject.getString("name")
                    city.cityCode = cityObject.getInt("id")
                    city.provinceId=provinceId
                    city.save()
                }
                return true
            }
            catch (e: JSONException){
                e.printStackTrace()
            }
        }
        return false
    }
    /*
  返回县级数据
   */
    fun handleCountryResponse(respose:String,cityId:Int):Boolean{
        if (!TextUtils.isEmpty(respose)){
            try {
                val  allcountry :JSONArray = JSONArray(respose)
                for ( i in 0..(allcountry.length()-1)){
                    var countryObject: JSONObject = allcountry.get(i) as JSONObject
                    var country:Country= Country()
                    country.countryName=countryObject.getString("name")
                    country.cityId = countryObject.getInt("id")
                    country.weatherId= countryObject.getString("weather_id")
                    country.cityId=cityId
                    country.save()
                }
                return true
            }
            catch (e: JSONException){
                e.printStackTrace()
            }
        }
        return false
    }

    /*
    将返回的json数据解析成weather的实体类
     */

    fun handleWeatherResponse(respose:String ):Weather{

        try {
            var  jsonObject :JSONObject = JSONObject(respose)
            var  jsonArray:JSONArray=jsonObject.getJSONArray("HeWeather6")
            var weatherContent:String=jsonArray.getJSONObject(0).toString()
            return Gson().fromJson(weatherContent,Weather::class.java)
        }
        catch (e: JSONException){
            e.printStackTrace()
        }
        return Weather();
    }

    /*
    将返回的json数据解析成aqi的实体类
     */

    fun handleAqiResponse(respose:String ): Aqi {

        try {
            var  jsonObject :JSONObject = JSONObject(respose)
            var  jsonArray:JSONArray=jsonObject.getJSONArray("HeWeather6")
            var weatherContent:String=jsonArray.getJSONObject(0).toString()
            return Gson().fromJson(weatherContent,Aqi::class.java)
        }
        catch (e: JSONException){
            e.printStackTrace()
        }
        return Aqi();
    }
}